package com.lubase.core.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.lubase.orm.mapper.MultiDatabaseMapper;
import com.lubase.orm.model.auto.DmDatabaseEntity;
import com.lubase.orm.multiDataSource.DBContextHolder;
import com.lubase.orm.multiDataSource.DatabaseConnectBuilder;
import com.lubase.orm.multiDataSource.DatabaseConnectModel;
import com.lubase.orm.multiDataSource.DynamicDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/datasource")
public class DataSourceController {
    @Autowired
    MultiDatabaseMapper multiDatabaseMapper;
    @Autowired
    DatabaseConnectBuilder databaseConnectBuilder;

    @Autowired
    DynamicDataSource dynamicDataSource;

    @RequestMapping(value = "/createDataSource", method = RequestMethod.POST)
    public Integer createDataSource(@RequestBody DmDatabaseEntity databaseEntity) {
        if (databaseEntity == null) {
            return 0;
        }
        DatabaseConnectModel database = databaseConnectBuilder.buildConnectModel(databaseEntity);
        Boolean result = null;
        try {
            result = dynamicDataSource.createDataSourceWithCheck(database);
            if (result) {
                DBContextHolder.setDataSourceCode(databaseEntity.getId().toString());
                multiDatabaseMapper.dropMonster(databaseEntity.getDatabase_name());
                multiDatabaseMapper.initMonsterTable(databaseEntity.getDatabase_name());
                multiDatabaseMapper.initMonsterData(databaseEntity.getDatabase_name());
                return 1;
            }
            return 0;
        } catch (Exception e) {
            log.error("注册数据源失败" + database.getAliasCode(), e);
            return 0;
        }
    }

    @RequestMapping(value = "/getMainDatabase", method = RequestMethod.GET)
    public String getMainDatabaseCode() {
        DruidDataSource druidDataSource = (DruidDataSource) dynamicDataSource.getResolvedDefaultDataSource();
        return druidDataSource.getRawJdbcUrl();
    }

    @RequestMapping(value = "/getAppDatabase", method = RequestMethod.GET)
    public List<String> getAppDatabaseCode() {
        Map<Object, DataSource> target = dynamicDataSource.getResolvedDataSources();
        List<String> list = new ArrayList<>();
        for (Object key : target.keySet()) {
            DruidDataSource druidDataSource = (DruidDataSource) target.get(key);
            list.add(key + ":" + druidDataSource.getRawJdbcUrl());
        }
        return list;
    }

    @RequestMapping(value = "/getCurrentDatabaseKey", method = RequestMethod.GET)
    public String getCurrentDatabaseKey() {
        return DBContextHolder.getDataSourceCode();
    }
}
